﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace thailandProject.Models
{
    // A class to create various utility methods
    public class UtilMethods
    {
        // simple method to check if a value is odd
        public static bool isOdd(int i)
        {
            try
            {
                if ((i % 2) != 0)
                    return true;
            }
            catch (Exception ex)
            {
                ErrorLog.logError(ex);
            }

            return false;
        }

        // Get first part of email to be able to create text wrap with email
        public static string getFirstPartEmail(string s)
        {
            string temp = s;
            try
            {
                int index = temp.IndexOf("@");
                if (index > 0)
                    temp = temp.Substring(0, index + 1);
            }
            catch (Exception ex)
            {
                ErrorLog.logError(ex);
            }

            return temp;
        }

        // Get last part of email to be able to create text wrap with email
        public static string getLastPartEmail(string s)
        {
            string temp = s;
            try
            {
                int index = temp.IndexOf("@");
                if (index > 0)
                    temp = temp.Substring(index, temp.Length);
            }
            catch (Exception ex)
            {
                ErrorLog.logError(ex);
            }

            return temp;
        }

        // Adds a carriage return after the '@' character in email.
        public static string stringWithBreak(string s)
        {
            string result = "";

            try
            {
                var builder = new StringBuilder();
                builder.Append("<strong>");
                foreach (char c in s)
                {
                    builder.Append(c);
                    if (c == '@')
                    {
                        builder.Append("</strong>");
                        builder.Append(@"<br />");
                    }
                }

                result = builder.ToString();
            }
            catch (Exception ex)
            {
                ErrorLog.logError(ex);
            }

            return result;
        }
    }
}